use crate::tool_result::Error;
use serde::{Deserialize, Serialize};
///json序列化
pub fn serialize<T: Serialize>(t: T) -> Result<String, Error> {
    match serde_json::to_string(&t) {
        Ok(serialized_data) => Ok(serialized_data),
        Err(e) => Err(Error::err(e.to_string())),
    }
}

///带json格式的序列化
pub fn serialize_pretty<T>(value: T) -> Result<String, Error>
where
    T: serde::Serialize,
{
    match serde_json::to_string_pretty(&value){
        Ok(serialized_data) => Ok(serialized_data),
        Err(e) => Err(Error::err(e.to_string())),
    }
}

///json反序列化
pub fn deserialize<T: for<'a> Deserialize<'a>>(json_str: String) -> Result<T, Error> {
    match serde_json::from_str(&json_str) {
        Ok(deserialized_data) => Ok(deserialized_data),
        Err(e) => Err(Error::err(e.to_string())),
    }
}
